package com.huanzhidadi.singleweb.delayqueue;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.DelayQueue;

@Data
@Slf4j
@AllArgsConstructor
public class DelayQueueProducer implements Runnable {

    private DelayQueue<DelayedUser> delayQueue;

    private Integer messageCount;

    private long delayedTime;

    // 为Thread结束提供标记
    private boolean stopFlag;

    @Override
    public void run() {
        for (int i=0; i<messageCount; i++) {
            try {
                DelayedUser delayedUser = new DelayedUser(new Random().nextInt(100)+"", delayedTime);
                log.info("put delayedUser {}", delayedUser);
                delayQueue.put(delayedUser);
                Thread.sleep(500l);
            } catch (Exception e) {
                log.error("生产者发生异常。", e);
            }
        }
        stopFlag = true;
    }
}
